// 03myCenter/02sectorization/03addressManage/newOreditAddress/newOreditAddress.js
var co = require('../../../../resource/js/common.js')
var app = getApp()
var p = 0, c = 0, d = 0
var _this

Page({

   /* 页面的初始数据*/
   data: {
      // 地址信息
      addressInfo: '',
      // 编辑时显示所填地区
      address: '',
      // 省市区选择器
      showDistpicker: false,
      provinceName: '',
      cityName: '',
      districtName: '',
      province: '',
      provinceSelIndex: '',
      citySelIndex: '',
      districtSelIndex: '',
      // 选择确认后省市区ID
      provinceCode: '',
      cityCode: '',
      districtCode: '',
      // 保存省市区ID
      Address_Province: '',
      Address_City: '',
      Address_Area: '',
      // 默认地址编辑时隐藏
      isDefault: true
   },
   /*生命周期函数--监听页面加载*/
   onLoad: function (options) {
      console.log("【获取默认地址】===", options.AddressIsDefault)
      _this = this
      // 存入地址信息
      if (JSON.stringify(options) != '{}') {
         if (options.AddressIsDefault == '1'){
            this.setData({
               isDefault:false
            })
         }
         this.setData({
            addressInfo: JSON.parse(options.addressInfo)
         })
         this.getProvince()
         this.getAllAreas()
      } else {
         this.setData({ address: '请选择' })
         this.getProvince()
      }
   },
   // 保存编辑获取新增地址
   formSubmit: function (e) {
      // console.log("【表单提交】===", e.detail.value)
      var dcd = this.data.districtCode          // 省市区ID
      console.log("【表单提交】===", dcd)
      // 手机正则表达式
      var telRule = /^1[3|4|5|7|8]\d{9}$/, nameRule = /^[\u2E80-\u9FFF]+$/
      var ad = this.data.addressInfo            //地址信息
      var adr = this.data.address               //显示的省市区
      var UN = e.detail.value.userName          //用户姓名
      var UP = e.detail.value.userPhone         //用户手机
      var ATA = e.detail.value.text             //详细地址信息
      var AID = e.detail.value.checkbox         //默认地址
      var add, adap,adac, ada
      if (AID.length == 0) {
         AID = 0
      } else {
         AID = 1
      }
      // 判断
      if (UN == '') {
         co.Modal("请填写收货人姓名")
      } else if (UP == '') {
         co.Modal("请填写手机号码")
      } else if (!telRule.test(UP)) {
         co.Modal("手机号格式不正确")
      } else if (adr == '请选择') {
         co.Modal("请选择所在地区")
      } else if (ATA == '') {
         co.Modal("请填写详细详地址")
      }else{
         if (ad == ''){
            add = ''
            adap = this.data.Address_Province
            adac = this.data.Address_City
            ada  = this.data.Address_Area
         } else if (this.data.Address_Province != ''){
            add = ad.Address_ID
            adap = this.data.Address_Province
            adac = this.data.Address_City
            ada = this.data.Address_Area
         }else{
            add = ad.Address_ID
            adap = ad.Address_Province
            adac = ad.Address_City
            ada = ad.Address_Area
         }
         var param = {
            User_ID: app.Data.userInfo.User_ID,
            Address_ID: add,
            Address_Province: adap,
            Address_City: adac,
            Address_Area: ada,
            Address_Detailed: ATA,
            Address_Name: UN,
            Address_Mobile: UP,
            Address_Is_Default: AID
         }
         co.getShareApi('user/Mg/saveAddress',param,function(res){
            // console.log("【获取返回信息】===",res.msg)
            wx.navigateBack({})
         })
      }
   },
   //   请求省的数据
   getProvince: function () {
      co.getShareApi('api/Areas/getProvince', {}, function (res) {
         if (res.code == 0) {
            _this.setData({ province: res.data })
            _this.setAreaData()
         }
      })
   },
   //   请求市的数据
   getCity: function (Pid, _function) {
      var param = {
         PID: Pid
      }
      co.getShareApi('api/Areas/getCity', param, function (res) {
         if (res.code == 0) {
            // _this.setData({ city: res.data });
            if (typeof (_function)) {
               _function(res.data)
            }
         }
      })
   },
   // 请求区县的数据
   getdistrict: function (Pid, Cid, _function) {
      var param = {
         PID: Pid,
         CID: Cid
      }
      co.getShareApi('api/Areas/getDistrict', param, function (res) {
         if (res.code == 0) {
            // _this.setData({ district: res.data })
            if (typeof (_function)) {
               _function(res.data)
            }
         }
      })
   },
   // 设置区域信息
   setAreaData: function (p, c, d) {
      var p = p || 0 // provinceSelIndex
      var c = c || 0 // citySelIndex
      var d = d || 0 // districtSelIndex
      // 设置省的数据
      var province = this.data.province
      var provinceName = []
      var provinceCode = []
      for (var item in province) {
         provinceName.push(province[item])
         provinceCode.push(item)
      }
      this.setData({
         provinceName: provinceName,
         provinceCode: provinceCode
      })
      // 设置市的数据
      var PID = provinceCode[p]
      this.getCity(PID, function (res) {
         var city = res;
         var cityName = []
         var cityCode = []
         for (var item in city) {
            cityName.push(city[item])
            cityCode.push(item)
         }
         _this.setData({
            cityName: cityName,
            cityCode: cityCode
         })
         var CID = cityCode[c]
         _this.getdistrict(PID, CID, function (res) {
            var district = res
            var districtName = []
            var districtCode = []
            for (var item in district) {
               districtName.push(district[item])
               districtCode.push(item)
            }
            _this.setData({
               districtName: districtName,
               districtCode: districtCode
            })
         })
      })
   },
   changeArea: function (e) {
      p = e.detail.value[0]
      c = e.detail.value[1]
      d = e.detail.value[2]
      this.setAreaData(p, c, d)
      // console.log(p, c, d)
   },
   // 点击选择所在地区
   selectArea: function () {
      this.setData({
         showDistpicker: true
      })
   },
   // 关闭按钮
   distpickerCancel: function () {
      this.setData({
         showDistpicker: false
      })
   },
   // 确认按钮
   distpickerSure: function () {
      this.setData({
         provinceSelIndex: p,
         citySelIndex: c,
         districtSelIndex: d,
         address: this.data.provinceName[p] + " " + this.data.cityName[c] + " " + this.data.districtName[d],
         Address_Province: this.data.provinceCode[p],
         Address_City: this.data.cityCode[c],
         Address_Area: this.data.districtCode[d]
      })
      if (this.data.districtName.length == 0) {
         this.setData({ indistrict: '' })
      }
      console.log("【地区的数组】====", this.data.districtName)
      // console.log("【选择的结构】", this.data.provinceName[p], this.data.cityName[c], this.data.districtName[d])
      this.distpickerCancel()
   },
   // 获取省市区
   getAllAreas() {
      var city = _this.data.addressInfo
      var param = {
         PID: city.Address_Province,
         CID: city.Address_City,
         AID: city.Address_Area
      }
      co.getShareApi('api/Areas/getFullPCD', param, function (res) {
         console.log("【获取省市区】===", res.data)
         if (res.code == 0) {
            _this.setData({ address: res.data })
         }
      })
   },
})